package com.shop.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.constant.MessageConstant;
import com.shop.constant.StatusConstant;
import com.shop.context.BaseContext;
import com.shop.dto.GoodsDTO;
import com.shop.dto.GoodsPageQueryDTO;
import com.shop.entity.Goods;
import com.shop.entity.GoodsFlavor;
import com.shop.exception.DeletionNotAllowedException;
import com.shop.mapper.GoodsFlavorMapper;
import com.shop.mapper.GoodsMapper;
import com.shop.mapper.SetmealGoodsMapper;
import com.shop.mapper.SetmealMapper;
import com.shop.result.PageResult;
import com.shop.service.GoodsService;
import com.shop.vo.GoodsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsFlavorMapper goodsFlavorMapper;
    @Autowired
    private SetmealGoodsMapper setmealGoodsMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 新增商品以及对应的口味数据
     * @param goodsDTO
     */
    @Override
    @Transactional
    public void saveWithFlavor(GoodsDTO goodsDTO) {

        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsDTO,goods);
        //向商品表添加1条数据
        goodsMapper.insert(goods);
        //向口味表插入n条数据
        Long goodsId = goods.getId();
        List<GoodsFlavor> flavors = goodsDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(goodsFlavor ->{
                goodsFlavor.setGoodsId(goodsId);
            });
            //向口味表插入n条数据
            goodsFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 商品分页查询
     * @param goodsPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(GoodsPageQueryDTO goodsPageQueryDTO) {
        PageHelper.startPage(goodsPageQueryDTO.getPage(),goodsPageQueryDTO.getPageSize());
        Page<GoodsVO> page = goodsMapper.pageQuery(goodsPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     *
     * 商品批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断当前商品能否删除---是否存在起售中的商品
        for (Long id : ids) {
            Goods goods= goodsMapper.getById(id);
            if (goods.getStatus() == StatusConstant.ENABLE) {
                //在起售状态不可以删除
                throw new DeletionNotAllowedException(MessageConstant.GOODS_ON_SALE);
            }
        }
        //判断当前商品能否删除---是否被套餐关联
        List<Long> setmealIds = setmealGoodsMapper.getSetmealIdsByGoodsIds(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
            //存在套餐关联商品
            throw new DeletionNotAllowedException(MessageConstant.GOODS_BE_RELATED_BY_SETMEAL);
        }
        //删除表中商品的数据
        for (Long id : ids) {
            goodsMapper.deleteByid(id);
            //删除商品关联的口味数据
            goodsFlavorMapper.deleteByGoodsId(id);
        }
    }

    /**
     *根据id查询商品
     * @param id
     * @return
     */
    @Override
    public GoodsVO getByidWithFlavor(Long id) {
        //根据id查询商品数据
        Goods goods = goodsMapper.getById(id);
        //根据商品id查询口味数据
        List<GoodsFlavor> goodsFlavor = goodsFlavorMapper.getByGoodsId(id);
        //将查询到的数据封装到VO
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goods,goodsVO);
        goodsVO.setFlavors(goodsFlavor);
        return goodsVO;
    }

    /**
     * 根据id修改商品对应的信息以及口味信息
     * @param goodsDTO
     */
    @Override
    @Transactional
    public void updateWithFlavor(GoodsDTO goodsDTO) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsDTO,goods);
        //修改商品表基本信息
        goodsMapper.update(goods);

        //删除原有的口味数据
        goodsFlavorMapper.deleteByGoodsId(goodsDTO.getId());
        //重新插入口味数据
        List<GoodsFlavor> flavors = goodsDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(goodsFlavor ->{
                goodsFlavor.setGoodsId(goodsDTO.getId());
            });
            //向口味表插入n条数据
            goodsFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 起售停售商品
     * @return
     */
    @Override
    public void startOrStop(Integer status, Long id) {
//        Goods Goods = new Goods();
//        Goods.setStatus(status);
//        Goods.setId(id);
        Goods goods = Goods.builder()
                .status(status)
                .id(id)
                .build();
        goodsMapper.update(goods);
     }

    /**
     * 根据分类id查询商品
     * @param categoryId
     * @return
     */
    @Override
    public List<Goods> list(Long categoryId) {
        Goods goods = Goods.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return goodsMapper.list(goods);
    }


    /**
     * 条件查询商品和口味
     * @param goods
     * @return
     */
    public List<GoodsVO> listWithFlavor(Goods goods) {
        List<Goods> goodsList = goodsMapper.list(goods);

        List<GoodsVO> goodsVOList = new ArrayList<>();

        for (Goods d : goodsList) {
            GoodsVO goodsVO = new GoodsVO();
            BeanUtils.copyProperties(d,goodsVO);

            //根据商品id查询对应的口味
            List<GoodsFlavor> flavors = goodsFlavorMapper.getByGoodsId(d.getId());

            goodsVO.setFlavors(flavors);
            goodsVOList.add(goodsVO);
        }

        return goodsVOList;
    }
}
